package com.enterpriseDigitalPlatform.util;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class DateTimeUtil { // 公用静态方法类

    private DateTimeUtil() {

    }

    // todo 时间戳对象转字符串
    public static String dateToString(LocalDateTime dateTime, String formatText) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatText);
        if (dateTime == null) {
            return "";
        }
        return dateTime.format(formatter);
    }

    // todo list<map<>> 创建修改时间全部转为日期字符串
    public static List<Map<String, Object>> dateToString(List<Map<String, Object>> list, String... keys) {
        for (Map<String, Object> m : list) {
            dateToString(m, keys);
        }
        return list;
    }

    // todo map<> 创建修改时间全部转为日期字符串
    public static Map<String, Object> dateToString(Map<String, Object> m, String... keys) {
        for (String key : keys) {
            if (m.get(key) != null) {
                m.put(key, dateToString((LocalDateTime) m.get(key), "yyyy-MM-dd HH:mm:ss"));
            }
        }
        return m;
    }


    // todo 13位时间戳转换时间字符串
    public static String timeStampToString(String s) {
        String res;
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        long lt = Long.parseLong(s);
        if (lt == 0) {
            return "";
        }
        // 将时间戳转换为时间
        LocalDateTime date = Instant.ofEpochMilli(lt).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        // 将时间调整为yyyy-MM-dd HH:mm:ss时间样式
        res = date.format(pattern);
        return res;
    }

    // todo 字符串转10位时间戳
    public static Long stringToTime(String time) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(time, ftf);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * 获取指定日期 的 零点时间戳
     *
     * @param date 日期。如  "2020-04-12"
     * @return 时间戳 秒
     */
    public static Long getStartOfdayByString(String date) {
        if (StringUtils.isEmpty(date)) {
            date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINA);
        LocalDate parse = LocalDate.parse(date, ofPattern);
        return parse.atStartOfDay().toInstant(ZoneOffset.of("+8")).getEpochSecond();
    }

    // 传时间对象直接返回开始时间戳秒级
    public static Long getStartOfDay(LocalDate date) {
        return date.atStartOfDay().toInstant(ZoneOffset.of("+8")).getEpochSecond();
    }

    /**
     * 获取指定日期 的 末尾时间戳
     *
     * @param date 日期。如  "2020-04-12"
     * @return 时间戳 秒
     */
    public static Long getEndOfDayByString(String date) {
        if (StringUtils.isEmpty(date)) {
            date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINA);
        LocalDate parse = LocalDate.parse(date, ofPattern);
        return LocalDateTime.of(parse, LocalTime.MAX).toInstant(ZoneOffset.of("+8")).getEpochSecond();
    }

    // 传时间对象直接返回结束时间戳 秒级
    public static Long getEndOfDay(LocalDate date) {
        return LocalDateTime.of(date, LocalTime.MAX).toInstant(ZoneOffset.of("+8")).getEpochSecond();
    }

    /**
     * LocalDateTime转String
     *
     * @param localDateTime 日期时间对象
     * @param pattern       格式示例： yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String LocalDateTimetoStr(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    public static String getNowTime() {
        return LocalDateTimetoStr(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");
    }

    public static Integer getNowTimeStamp() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    // 判断是否同一天
    public static boolean isTwoDay(int timestamp1, int timestamp2) {
        // 将秒级时间戳转换为毫秒级时间戳
        long millis1 = timestamp1 * 1000L;
        long millis2 = timestamp2 * 1000L;

        // 将毫秒级时间戳转换为LocalDate
        LocalDate date1 = Instant.ofEpochMilli(millis1).atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate date2 = Instant.ofEpochMilli(millis2).atZone(ZoneId.systemDefault()).toLocalDate();

        // 计算两个日期之间的天数差
        return Math.abs(date2.toEpochDay() - date1.toEpochDay()) < 2;
    }

}
